package com.jwm.platform.db;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

public class BaseModel {

	public void setBean(Map<String, Object> m) throws Exception {

		Field[] fields = this.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			fieldAttribute fatt = (fieldAttribute) fields[i].getAnnotation(fieldAttribute.class);

			fields[i].setAccessible(true);

			Object v = m.get(fatt.name());

			if (v != null) {

				if (fields[i].getType().equals(java.util.Date.class)) {
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					v = format.parse(v.toString());
				}
				if (v.getClass().equals(Integer.class) && fields[i].getType().equals(String.class)) {
					v = v.toString();
				}
				if (v.getClass().equals(Double.class) && fields[i].getType().equals(float.class)) {
					Double d = (Double) v;
					v = d.floatValue();
				}
				if (v.getClass().equals(BigDecimal.class) && fields[i].getType().equals(float.class)) {
					BigDecimal d = (BigDecimal) v;
					v = d.floatValue();
				}
				if (fields[i].getType().equals(int.class)) {
					v = Integer.parseInt(v.toString().equals("") ? "0" : v.toString());
				}
                fields[i].set(this, v);
			}
		}
	}

	public String getMessage() {
		return "";
	}
}
